package com.horizon.im.imClient.clientSender;

import com.horizon.concurrent.CallBackTask;
import com.horizon.concurrent.CallBackTaskScheduler;
import com.horizon.concurrent.FutureTaskScheduler;
import com.horizon.im.common.bean.UserDTO;
import com.horizon.im.common.bean.msg.ProtoMsg;
import com.horizon.im.imClient.client.ClientSession;
import com.horizon.im.imClient.client.CommandController;
import io.netty.channel.ChannelFuture;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@Data
@Slf4j
public class BaseSender {
    private UserDTO user;
    private ClientSession session;

    @Autowired
    protected CommandController commandController;

    public boolean isConnected() {
        if(null == session) {
            log.info("session is null");
            return false;
        }
        return session.isConnected();
    }

    public boolean isLogin()
    {
        if (null == session)
        {
            log.info("session is null");
            return false;
        }

        return session.isLogin();
    }

    public void sendMsg(ProtoMsg.Message msg) {
        CallBackTaskScheduler.add(new CallBackTask<Boolean>() {
            @Override
            public Boolean execute() throws Exception {
                if (null == getSession()) {
                    throw new Exception("session is null");
                }
                if (!isConnected()){
                    log.info("链接还没成功");
                    throw new Exception("链接还没成功");
                }

                final Boolean[] isSussess = {false};
                ChannelFuture f = getSession().witeAndFlush(msg);
                f.addListener(new GenericFutureListener<Future<? super Void>>() {
                    @Override
                    public void operationComplete(Future<? super Void> future) throws Exception {
                        //回调
                        if (future.isSuccess()) {
                            isSussess[0] = true;
                            log.info("操作成功");
                        }
                    }
                });

                try {
                    f.sync();//同步阻塞
                } catch (InterruptedException e) {
                    isSussess[0] = false;
                    e.printStackTrace();
                    throw new Exception("error occur");
                } finally {

                }
                return isSussess[0];
            }

            @Override
            public void onBack(Boolean aBoolean) {
                if (aBoolean)
                {
                    BaseSender.this.sendSucced(msg);

                } else
                {
                    BaseSender.this.sendfailed(msg);

                }
            }

            @Override
            public void onException(Throwable t) {
                BaseSender.this.sendException(msg);
            }
        });
    }

    protected void sendSucced(ProtoMsg.Message message)
    {
        log.info("发送成功");

    }

    protected void sendfailed(ProtoMsg.Message message)
    {
        log.info("发送失败");
    }

    protected void sendException(ProtoMsg.Message message)
    {
        log.info("发送消息出现异常");

    }
}
